<?php
namespace app\user\controller;

use app\common\controller\Front;

class Recharge extends Front
{
    protected function initialize()
    {
        $this->auth['check'] = true;

        $this->auth['cancel_login'] = false;
        
        $this->auth['cancel_limit'] = '*';
        
        parent::initialize(); 
    }

    public function index()
    {
        $post = [];
        $post['pay_name']      = config('site.name').'积分充值';//商品描述
        $post['pay_quantity']  = 1;//数量
        $post['pay_price']     = sprintf("%.2f",input('request.price/s', 1));//单价
        $post['pay_total_fee'] = $post['pay_price']*$post['pay_quantity'];//总价
        $post['notify_url']    = url('share/alipay/notify',false,false,true);//异步通知
        $post['return_url']    = url('user/order/index',false,false,true);//同步通知
        $post['pay_info_id']   = 0;//内容ID
        $post['pay_user_id']   = $this->user['user_id'];//用户ID
        $post['pay_platform']  = 'alipay';//支付平台
        $post['pay_model']     = 'recharge';//内容模型
        $post['pay_scene']     = 'pc';//支付场景
        //是否手机支付
        if($this->request->isMobile()){
            $post['pay_scene'] = 'wap';
        }
        //发起支付
        if($result = $this->paySubmit($post)){
            return $result;
        }
        //支付失败
        $this->error('出错了，请重试！', 'user/index/index');
    }

    /**
     * 根据订单信息发起支付
     * @version 1.0.0 首次引入
     * @param array $post 必需;数组格式;默认：空
     * @return mixed false|支付表单
     */
    private function paySubmit($post=[])
    {
        //充值金额验证
        if($post['pay_total_fee'] < 0.01){
            $this->error('支付金额不足1分');
        }
        //订单信息
        $post = model('common/Pay','loglic')->createOrder($post);
        //手机支付
        if($post['pay_scene']=='wap'){
            return model('common/Alipay','loglic')->createWap($post);
        }
        //电脑支付
        return model('common/Alipay','loglic')->create($post);
    }
}